package com.miracle.java2110.controller;

import com.miracle.java2110.QiniuUtilsGuest;
import com.miracle.java2110.QinuiUtils;
import com.miracle.java2110.common.PageVo;
import com.miracle.java2110.common.R;
import com.miracle.java2110.pojo.Guest;
import com.miracle.java2110.service.GuestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/guest")
@CrossOrigin("*")
public class GuestController {

    @Autowired
    private GuestService guestService;

    @RequestMapping("/list")
    public List<Guest> findGuests() {
        return guestService.findGuests();
    }

    @RequestMapping("/pageData")
    public PageVo<Guest> getPageData(@RequestParam Map<String, Object> params) {
        return guestService.getPageData(params);
    }

    @RequestMapping("/update")
    public R updateById(Guest guest) {
        return guestService.updateById(guest);
    }

    @RequestMapping("/delete")
    public R deleteById(Integer gid) {
        return guestService.deleteById(gid);
    }

    @RequestMapping("/insert")
    public R insertUser(Guest guest) {
        return guestService.insertGuest(guest);
    }

    @RequestMapping("/deletes")
    public R deleteByIds(Integer[] gids) {
        return guestService.deleteList(gids);
    }

    @RequestMapping("/upload")
    public R uploadPic(MultipartFile file, HttpServletRequest request) throws IOException {
        String filename = file.getOriginalFilename();

        System.out.println(filename);

        String suffix = filename.substring(filename.lastIndexOf("."));

        String fileName = UUID.randomUUID().toString() + suffix;

        byte[] bytes = file.getBytes();

        QiniuUtilsGuest.uploadBytes2Qiniu(bytes, fileName);

        return R.ok(200, "上传成功", "http://r23hnwddi.hn-bkt.clouddn.com/" + fileName);
    }

}
